<%@Page Language="VB"%>

<%@Import Namespace="System.Data" %>
<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<%@ Register TagPrefix="wrox" TagName="getdataview" Src="..\global\get-dataview-control.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Handling Data Binding Events in a DataList Object</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:9pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}

.rHead {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial;
        font-size:14pt; font-weight:bold; padding:8px; color:green}
.rItem {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial,sans-serif;
        font-size:10pt}
.rFoot {font-family:Tahoma,Arial; font-size:8pt; padding:8px; color:darkgray}
.bigRed {font-family:Arial Black,sans-serif; font-size:12pt; font-weight:normal; color:red}

</style></head>
<body bgcolor="#ffffff">
<span class="heading">Handling Data Binding Events in a DataList Object</span><hr />
<!--------------------------------------------------------------------------->

<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server" />

<%'-- insert the control that creates the DataSet --%>
<wrox:getdataview id="ctlDataView" runat="server" />

<ASP:DataList id="MyDataList" runat="server" RepeatLayout="Table"
     OnItemDataBound="CheckTitle">

  <HeaderTemplate>
    <div class="rHead">
      Some of the Latest Wrox Press Books<br />
      <img src="images/redrule.gif">
    </div>
  </HeaderTemplate>

  <ItemTemplate>
    <div class="rItem">
      <b><ASP:Label id="TitleLabel" runat="server"
              Text='<%# Container.DataItem("Title") %>' /></b><br />
      * ISBN: <%# Container.DataItem("ISBN") %> &nbsp;
      Published: <%# Container.DataItem("PublicationDate") %>
    </div>
  </ItemTemplate>

  <FooterTemplate>
    <img src="images/redrule.gif">
    <div class="rFoot">
      For more information visit <a href="http://www.wrox.com">http://www.wrox.com</a>
    </div>
  </FooterTemplate>

</ASP:DataList><p />

<!--------------------------------------------------------------------------->

<script language="vb" runat="server">

Sub Page_Load()

   If Not Page.IsPostback Then   'fill the DataList from a DataView object

      'get connection string from ..\global\connect-strings.ascx user control
      Dim strConnect As String  = ctlConnectStrings.OLEDBConnectionString

      'create a SQL statement to select some rows from the database
      Dim strSelect As String
      strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '18610026%'"

      'create a variable to hold an instance of a DataView object
      Dim objDataView As DataView

      'get dataset from get-dataset-control.ascx user control
      objDataView = ctlDataView.GetDataView(strConnect, strSelect)

      If IsNothing(objDataView) Then Exit Sub

      'set the DataSource property of the DataList
      MyDataList.DataSource = objDataView

      'and bind the control to the data
      MyDataList.DataBind()

   End If

End Sub


Sub CheckTitle(objSender As Object, objArgs As DataListItemEventArgs)

   'see what type of row (header, footer, item, etc.) caused the event
   Dim objItemType As ListItemType = CType(objArgs.Item.ItemType, ListItemType)

   'only format the results if it's an Item or AlternatingItem event
   If objItemType = ListItemType.Item _
   Or objItemType = ListItemType.AlternatingItem Then

      'objArgs.Item.DataItem returns the data for this row of items
      Dim objRowVals As DataRowView = CType(objArgs.Item.DataItem, DataRowView)

      'get the value of the Title column
      Dim strTitle As String = objRowVals("Title")

      If strTitle.IndexOf("Active Server Pages") >= 0 _
      Or strTitle.IndexOf("ADO") >= 0 Then

         'get a reference to the "Title" ASP:Label control in this row
         Dim objLabel As Label = CType(objArgs.Item.FindControl("TitleLabel"), Label)

         'add a message to this Label control
         objLabel.Text += " &nbsp; <span class='bigRed'>Great for ASP Programmers!</span>"

      End If

   End If

End Sub

</script>

<!--------------------------------------------------------------------------->
<hr /><span class="cite">[<a href="../global/viewsource.aspx">view source</a>] &nbsp; &nbsp; &nbsp; &nbsp;
&copy;2001 <a class="cite" href="http://www.wrox.com/">Wrox Press</a> -
<a class="cite" href="http://www.wrox.com/Books/Book_Details.asp?isbn=1861004885">Professional ASP.NET</a> (ISBN: 1-861004-88-5)</span>
</body>
</html>
